package com.alex.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.AbsoluteLayout.LayoutParams;

import com.alex.ui.ass.IAdapter;
import com.alex.ui.ass.Recycler;
import com.alex.ui.ass.ViewWithShadowAttr;
import com.helios.baseFunction.Define.KeyCode;
import com.helios.protocol.Anim;
import com.helios.ui.IView;
import com.helios.ui.MAbsoluteLayout;
import com.helios.ui.Util;

@SuppressWarnings({ "unused", "deprecation" })
public abstract class MGroupView extends MAbsoluteLayout {
	
	/**
	 * 是否已获取到尺寸信息
	 */
	protected boolean mGainedSize;
	/**
	 * 当前宽度，剪切域
	 */
	protected int mCurrentWidth;
	/**
	 * 当前高度，剪切域
	 */
	protected int mCurrentHeight;
	
	/**
	 * 数据，视图适配器
	 */
	protected IAdapter mAdapter;
	
	/**
	 * 元素容器的容器，放置非聚焦容器和聚焦容器
	 */
	protected MAbsoluteLayout mViewContent;
	/**
	 * 非聚焦元素容器
	 */
	protected MAbsoluteLayout mViewNormalContent;
	/**
	 * 聚焦元素容器
	 */
	protected MAbsoluteLayout mViewFocusedContent;
	
	/**
	 * 是否使用全局焦点
	 */
	protected boolean mGlobalFocus;
	/**
	 * 焦点视图
	 */
	protected IView mViewFocus;
	/**
	 * GroupView与FocusView之间的水平相对位置差
	 */
	protected int mOffsetX;
	/**
	 * GroupView与FocusView之间的垂直相对位置差
	 */
	protected int mOffsetY;
	
	/**
	 * 元素视图的信息
	 */
	protected ViewWithShadowAttr mAttrItem;

	/**
	 * 焦点视图的信息
	 */
	protected ViewWithShadowAttr mAttrFocus;
	
	/**
	 * 被选中的元素索引
	 */
	protected int mSelected = INVALIDATE_INDEX;
	/**
	 * 被聚焦的元素索引
	 */
	protected int mFocused = INVALIDATE_INDEX;
	
	/**
	 * 复用回收器
	 */
	protected Recycler<Integer, IView> mRecycler = new Recycler<Integer, IView>();
	
	/**
	 * 非法的索引
	 */
	public static final int INVALIDATE_INDEX = -1;
	
	public MGroupView(Context context) {
		super(context);
		
		init();
	}

	public MGroupView(Context context, AttributeSet attrs) {
		super(context, attrs);

		init();
	}

	public MGroupView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);

		init();
	}
	
	@Override
	public void setMFocus(boolean focused) {
		super.setMFocus(focused);
Log.v("xlh*MGroupView*setMFocus", "mGlobalFocus="+mGlobalFocus);
		if (null != mViewFocus) {
			if (mGlobalFocus) {
				if (focused) {
					onAttachFocusView();
				}

				IView itemView = getFocusedItemView();
				
				if (null != itemView) {
					focusItem(itemView, focused);
				}
			} else {
				IView itemView = getFocusedItemView();
				
				if (null != itemView) {
					focusItem(itemView, focused);
				}

				mViewFocus.setVisibility(focused ? VISIBLE : INVISIBLE);
			}
		}
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		
		mGainedSize = true;
		mCurrentWidth = Util.convertOut(w);
		mCurrentHeight = Util.convertOut(h);
		
		onAttachItemView();
		onAttachFocusView();
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (!hasMFocus()
				|| null==mAdapter
				|| mAdapter.getCount()<=0) {
			return false;
		}
		
		return onKeyCode(event.getAction(), KeyCode.getKeyCode(event));
	}
	
	public final void setAdapter(IAdapter adapter) {
		Log.v("xlh*MGroupView*setAdapter", "adapterCount="+adapter.getCount());
		mAdapter = adapter;
		mViewNormalContent.removeAllViews();
		mViewFocusedContent.removeAllViews();
		
//		这两个绘制item的函数在子类 MGridView 里面实现 
		onAttachItemView();
		onAttachFocusView();
	}
	
	public final void setSingleFocusView(IView focusView, ViewWithShadowAttr attr) {
		if (null!=mViewFocus && !mGlobalFocus) {
			removeIView(mViewFocus);
		}

		mViewFocus = focusView;
		mAttrFocus = attr;
		mGlobalFocus = false;
		
		onAttachFocusView();
	}
	
//	设置聚焦view
	public final void setGlobalFocusView(IView focusView, ViewWithShadowAttr attr, 
			int offsetX, int offsetY) {
		Log.v("xlh*MGroupView*setGlobalFocusView", "mOffsetX="+offsetX+"/mOffsetY"+mOffsetY);
		if (null!=mViewFocus && !mGlobalFocus) {
			removeIView(mViewFocus);
		}
		
		mViewFocus = focusView;
		mAttrFocus = attr;
		mGlobalFocus = true;
		mOffsetX = offsetX;
		mOffsetY = offsetY;
		
//		在容器里绘制聚焦view，在子类MGridView里面实现
		onAttachFocusView();
	}
//	选中放大效果
	protected void focusItem(IView view, boolean focused) {
		if (focused) {
			Anim.scaleFocused(view, 
					Util.convertIn(mAttrItem.paddingLeft+mAttrItem.scalePrivotX), 
					Util.convertIn(mAttrItem.paddingTop+mAttrItem.scalePrivotY));
		} else {
			Anim.scaleNormal(view);
		}
	}
	
	public abstract void setSelected(int index);
	
	protected abstract void animFocusView(IView view, LayoutParams params, boolean scaled);
	
	protected abstract void onAttachItemView();
	
	protected abstract void onAttachFocusView();
	
	protected abstract boolean onKeyCode(int action, int keyCode);
	
	protected abstract IView getFocusedItemView();

	private void init() {
		mViewContent = new MAbsoluteLayout(getContext());
		addIView(mViewContent);
		mViewNormalContent = new MAbsoluteLayout(getContext());
		mViewContent.addIView(mViewNormalContent, 
				new LayoutParams(LayoutParams.MATCH_PARENT, 
						LayoutParams.MATCH_PARENT, 
						0, 0));
		mViewFocusedContent = new MAbsoluteLayout(getContext());
		mViewContent.addIView(mViewFocusedContent, 
				new LayoutParams(LayoutParams.MATCH_PARENT, 
						LayoutParams.MATCH_PARENT, 
						0, 0));
	}
}
